package com.sthz.facepay.mapper;

import com.sthz.facepay.model.Advertisement;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.apache.ibatis.type.JdbcType;

import java.util.List;

public interface AdvertisementMapper {
    @Delete({
            "delete from st_advertisement",
            "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    @Insert({
            "insert into st_advertisement (id, aid, ",
            "title, picture, ",
            "today_exposure, total_exposure, ",
            "total_money, surplus_money, ",
            "monovalent, data_time, ",
            "link, content)",
            "values (#{id,jdbcType=INTEGER}, #{aid,jdbcType=INTEGER}, ",
            "#{title,jdbcType=VARCHAR}, #{picture,jdbcType=VARCHAR}, ",
            "#{today_exposure,jdbcType=INTEGER}, #{total_exposure,jdbcType=INTEGER}, ",
            "#{total_money,jdbcType=DOUBLE}, #{surplus_money,jdbcType=DOUBLE}, ",
            "#{monovalent,jdbcType=DOUBLE}, #{data_time,jdbcType=TIMESTAMP}, ",
            "#{link,jdbcType=VARCHAR}, #{content,jdbcType=VARCHAR})"
    })
    int insert(Advertisement record);

    @InsertProvider(type=AdvertisementSqlProvider.class, method="insertSelective")
    int insertSelective(Advertisement record);

    @Select({
            "select",
            "id, aid, title, picture, today_exposure, total_exposure, total_money, surplus_money, ",
            "monovalent, data_time, link, content",
            "from st_advertisement",
            "where id = #{id,jdbcType=INTEGER}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="aid", property="aid", jdbcType=JdbcType.INTEGER),
            @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
            @Result(column="picture", property="picture", jdbcType=JdbcType.VARCHAR),
            @Result(column="today_exposure", property="today_exposure", jdbcType=JdbcType.INTEGER),
            @Result(column="total_exposure", property="total_exposure", jdbcType=JdbcType.INTEGER),
            @Result(column="total_money", property="total_money", jdbcType=JdbcType.DOUBLE),
            @Result(column="surplus_money", property="surplus_money", jdbcType=JdbcType.DOUBLE),
            @Result(column="monovalent", property="monovalent", jdbcType=JdbcType.DOUBLE),
            @Result(column="data_time", property="data_time", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="link", property="link", jdbcType=JdbcType.VARCHAR),
            @Result(column="content", property="content", jdbcType=JdbcType.VARCHAR)
    })
    Advertisement selectByPrimaryKey(Integer id);

    @UpdateProvider(type=AdvertisementSqlProvider.class, method="updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(Advertisement record);

    @Update({
            "update st_advertisement",
            "set aid = #{aid,jdbcType=INTEGER},",
            "title = #{title,jdbcType=VARCHAR},",
            "picture = #{picture,jdbcType=VARCHAR},",
            "today_exposure = #{today_exposure,jdbcType=INTEGER},",
            "total_exposure = #{total_exposure,jdbcType=INTEGER},",
            "total_money = #{total_money,jdbcType=DOUBLE},",
            "surplus_money = #{surplus_money,jdbcType=DOUBLE},",
            "monovalent = #{monovalent,jdbcType=DOUBLE},",
            "data_time = #{data_time,jdbcType=TIMESTAMP},",
            "link = #{link,jdbcType=VARCHAR},",
            "content = #{content,jdbcType=VARCHAR}",
            "where id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Advertisement record);

    /**
     * 查询所有
     * @return
     */
    @Select({
            "select * from st_advertisement where aid = #{aid,jdbcType=INTEGER}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="aid", property="aid", jdbcType=JdbcType.INTEGER),
            @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
            @Result(column="picture", property="picture", jdbcType=JdbcType.VARCHAR),
            @Result(column="today_exposure", property="today_exposure", jdbcType=JdbcType.INTEGER),
            @Result(column="total_exposure", property="total_exposure", jdbcType=JdbcType.INTEGER),
            @Result(column="total_money", property="total_money", jdbcType=JdbcType.DOUBLE),
            @Result(column="surplus_money", property="surplus_money", jdbcType=JdbcType.DOUBLE),
            @Result(column="monovalent", property="monovalent", jdbcType=JdbcType.DOUBLE),
            @Result(column="data_time", property="data_time", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="link", property="link", jdbcType=JdbcType.VARCHAR),
            @Result(column="content", property="content", jdbcType=JdbcType.VARCHAR),
            @Result(column="aid", property="agent", one=@One(select="com.sthz.facepay.mapper.AgentMapper.getById",fetchType = FetchType.EAGER))
    })
    Advertisement findAll(int aid);

    /**
     * 根据ID查询数据
     * @return
     */
    @Select({
            "select * from st_advertisement a inner join st_agent t on a.aid = t.id where  st_advertisement.id=#{id,jdbcType=INTEGER}"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="aid", property="aid", jdbcType=JdbcType.INTEGER),
            @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
            @Result(column="picture", property="picture", jdbcType=JdbcType.VARCHAR),
            @Result(column="today_exposure", property="today_exposure", jdbcType=JdbcType.INTEGER),
            @Result(column="total_exposure", property="total_exposure", jdbcType=JdbcType.INTEGER),
            @Result(column="total_money", property="total_money", jdbcType=JdbcType.DOUBLE),
            @Result(column="surplus_money", property="surplus_money", jdbcType=JdbcType.DOUBLE),
            @Result(column="monovalent", property="monovalent", jdbcType=JdbcType.DOUBLE),
            @Result(column="data_time", property="data_time", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="link", property="link", jdbcType=JdbcType.VARCHAR),
            @Result(column="content", property="content", jdbcType=JdbcType.VARCHAR),
            @Result(column="aid", property="agent", one=@One(select="com.sthz.payment.getById"))
    })

    Advertisement findById(int id);

    /**
     * title模糊查询
     * @return
     */
    @Select({
            "select * from st_advertisement( a inner join st_agent t on a.aid = t.id) where  title like '%'"
    })
    @Results({
            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="aid", property="aid", jdbcType=JdbcType.INTEGER),
            @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
            @Result(column="picture", property="picture", jdbcType=JdbcType.VARCHAR),
            @Result(column="today_exposure", property="today_exposure", jdbcType=JdbcType.INTEGER),
            @Result(column="total_exposure", property="total_exposure", jdbcType=JdbcType.INTEGER),
            @Result(column="total_money", property="total_money", jdbcType=JdbcType.DOUBLE),
            @Result(column="surplus_money", property="surplus_money", jdbcType=JdbcType.DOUBLE),
            @Result(column="monovalent", property="monovalent", jdbcType=JdbcType.DOUBLE),
            @Result(column="data_time", property="data_time", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="link", property="link", jdbcType=JdbcType.VARCHAR),
            @Result(column="content", property="content", jdbcType=JdbcType.VARCHAR)
    })
    List<Advertisement> findBylike();
}